"""
   ┌─┐       ┌─┐ + +
┌──┘ ┴───────┘ ┴──┐++
│                 │
│       ───       │++ + + +
███████───███████ │+
│                 │+
│       ─┴─       │
│                 │
└───┐         ┌───┘
    │         │
    │         │   + +
    │         │
    │         └──────────────┐
    │                        │
    │                        ├─┐
    │                        ┌─┘
    │                        │
    └─┐  ┐  ┌───────┬──┐  ┌──┘  + + + +
      │ ─┤ ─┤       │ ─┤ ─┤
      └──┴──┘       └──┴──┘  + + + +
             神兽保佑
            代码无BUG!
"""
import pandas as pd


class ArticleDynamic:
    def __init__(self):
        self.item_tag = {}
        self.hobby_tag = {}
        self.dynamic_Frame = pd.DataFrame(columns=["moment_id", "moment_content", "topic", "tag", "兴趣标签"])
        self.article_Frame = pd.DataFrame(columns=["moment_id", "title", "moment_content", "topic", "tag", "兴趣标签"])

    def read_file(self):
        # 读取文件
        item_tag_file = pd.read_excel("./file/话题标签映射表.xls", sheet_name="项目话题")
        hobby_tag_file = pd.read_excel("./file/话题标签映射表.xls", sheet_name="兴趣话题")
        for item in item_tag_file.values:
            self.item_tag[item[0]] = str(item[1])
        for hobby in hobby_tag_file.values:
            self.hobby_tag[hobby[0]] = str(hobby[1])

        print(self.item_tag)
        print(self.hobby_tag)

    def read_article(self):
        # 读取文章内容
        dynamic_content = pd.read_excel("./file/需要加标签的内容.xlsx", sheet_name="动态内容").values
        article_content = pd.read_excel("./file/需要加标签的内容.xlsx", sheet_name="文章内容").values
        print("开始处理动态")
        self.dynamic_process(dynamic_content)
        print("开始处理文章")
        self.article_process(article_content)

    def dynamic_process(self, dynamic_content):
        for con in dynamic_content:
            topic_list = str(con[2]).split(",")
            if pd.isnull(con[4]):
                hobby_tag = ""
            else:
                hobby_tag = str(con[4])
            if pd.isnull(con[3]):
                item_tag = ""
            else:
                item_tag = str(con[3])
            for topic in topic_list:
                if topic in self.hobby_tag.keys():
                    hobby_tag += self.hobby_tag[topic] + ","
                elif topic in self.item_tag.keys():
                    item_tag += self.item_tag[topic] + ","
            self.dynamic_Frame = self.dynamic_Frame.append(
                pd.DataFrame({"moment_id": [con[0]], "moment_content": [con[1]], "topic": [con[2]], "tag": [item_tag],
                              "兴趣标签": [hobby_tag]})
            )
        self.dynamic_Frame.to_excel("动态内容.xlsx", '动态内容', index=False)

    def article_process(self, article_content):
        for con in article_content:
            topic_list = str(con[3]).split(",")
            if pd.isnull(con[5]):
                hobby_tag = ""
            else:
                hobby_tag = str(con[5])
            if pd.isnull(con[4]):
                item_tag = ""
            else:
                item_tag = str(con[4])
            for topic in topic_list:
                if topic in self.hobby_tag.keys():
                    hobby_tag += self.hobby_tag[topic] + ","
                elif topic in self.item_tag.keys():
                    item_tag += self.item_tag[topic] + ","
            self.article_Frame = self.article_Frame.append(
                pd.DataFrame({"moment_id": [con[0]], "title": [con[1]], "moment_content": [con[2]], "topic": [con[3]],
                              "tag": [item_tag],
                              "兴趣标签": [hobby_tag]})
            )
        self.article_Frame.to_excel("文章内容.xlsx", '文章内容', index=False)

    def run(self):
        self.read_file()
        self.read_article()


if __name__ == '__main__':
    ar = ArticleDynamic()
    ar.run()
